(function(){
		var pass= new Array();
		var ispass=1;
		var $err=$("<span class='tips'></span>");
		$.fn.tgTextValidation=function(p){
			var vStr=$(this).attr("validation");
			var reg = new RegExp();
			var $this=$(this);

			var infostr=$this.attr("title");
			
			if(p){
				var i=getIndex(p,$this);
			}
			
			function insertErr(str,obj){
				if(infostr){
					$err.clone().html(infostr).insertAfter(obj);
				}else{
					$err.clone().html(str).insertAfter(obj);
				}	
			}
			
			$this.focusout(function(){
				$this.next("span").remove();
				if(vStr.indexOf("-")>=0){
					if(vStr.split("-")[0]=="l"){
						var l=parseInt(vStr.split("-")[1]);
						if(vStr.indexOf("nb")>=0){
							if($this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("内容不能为空",$this);
								pass[i]=0;
							}else{
								cl($this.val(),l,i);
							}
						}else{
							cl($this.val(),l,i);
						}							
					}else if(vStr.split("-")[1]=="nb"){
						if($this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
							insertErr("内容不能为空",$this);
							pass[i]=0;
						}else{
							valiStr(vStr.split("-")[0],i);
						}
					}	
				}else{
					valiStr(vStr,i);
				}
			});
			
			//验证长度
			function cl(str,length,i){
				if(str.length>length){
					insertErr("不得多于"+vStr.split("-")[1]+"个字",$this);
					pass[i]=0;							
				}else{
					$this.next("span").remove();
					pass[i]=1;
				}
			}
			//验证字符串
			function valiStr(string,i){
				switch(string)
					{
							case "zh":
							reg=new RegExp("[u4e00-u9fa5]");
							if(reg.test($this.val())|| $this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("请输入中文",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}				
							break;
							
							case "en":
							reg= new RegExp("^[A-Za-z]+$");
							if(!reg.test($this.val())){
								insertErr("请输入英文",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "code":
							reg = new RegExp("^[A-Za-z0-9]+$");
							if(!reg.test($this.val())){
								insertErr("请输入字母或数字",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "email":
							reg= new RegExp("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,}){1,2})$");
							if(!reg.test($this.val())){
								insertErr("请输入正确的电子邮箱地址",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "url":
							reg=new RegExp("((^http)|(^https)):\/\/(\\w)+\.(\\w)+");
							if(!reg.test($this.val())){
								insertErr("请输入url",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "id":
							reg= new RegExp("^[0-9]{17}[0-9xX]$|^[0-9]{14}[0-9xX]$");
							if(!reg.test($this.val())|| $this.val().replace(/(^\s*)|(\s*$)/g,"")==""){
								insertErr("请输入正确的身份证号码",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "phone":
							var str = $this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg = new RegExp("^(\\d{3,4}-)?(\\d{7,8})(-\\d*)?$|(1[0-9]{10})$");
							if(!reg.test(str)){
								insertErr("请输入有效电话或手机号码",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;
							}
							break;
							
							case "mobile":
							var s = $this.val().replace(/(^\s*)|(\s*$)/g,"");
							var reg = new RegExp("^(1[0-9]{10})$");
							if(!reg.test(s)){
								insertErr("请输入有效手机号码",$this);
								pass[i]=0;
							}else{
								$this.next("span").remove();
								pass[i]=1;	
							}
							break;
							
							case "num":
							reg = new RegExp("^[0-9]*$");
							if($this.val().replace(/(^\s*)|(\s*$)/g,"")!=""){
								if(!reg.test($this.val())){
									insertErr("请输入数字",$this);
									pass[i]=0;
								}else{
									$this.next("span").remove();
									pass[i]=1;
								}
							}
							
							break;
							
							case "Name":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg=new RegExp("^[A-Za-z\u4e00-\u9fa5.]*$");
							if(s){
								if(reg.test(s)){
									if(s.length>10){
										insertErr("请输入20位以内的中英文和‘.’",$this);	
										pass[i]=0;	
									}else{
										pass[i]=1;	
									}	
								}else{
									insertErr("请输入20位以内的中英文和‘.’",$this);	
									pass[i]=0;	
								}
							}else{
								$err.clone().html("内容不能为空")	.insertAfter($this);
								pass[i]=0;
							}
														
							break;
							
							case "accountNum":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							reg = new RegExp("^[0-9]{10,30}$");
							if(s){
								if(reg.test(s)){
									pass[i]=1;	
								}else{
									insertErr("请输入有效账号",$this);										
									pass[i]=0;		
								}
							}else{
								$err.clone().html("内容不能为空")	.insertAfter($this);
								pass[i]=0;
							}
							break;
							
							case "comName":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							if(s){
								s=s.replace(/[0-9]/g,"");
								if(s){
									if(s.length>50){
										insertErr("不能超过50个字符",$this);										
										pass[i]=0;	
									}else{
										pass[i]=1;
									}	
								}else{
									insertErr("不能全为数字",$this);									
									pass[i]=0;	
								}	
							}else{
								insertErr("内容不能为空",$this);										
								pass[i]=0;
							}
							break;
							
							case "username":
							var s=$this.val().replace(/(^\s*)|(\s*$)/g,"");
							var i=0;
							reg=new RegExp("[u4e00-u9fa5]");
							for(var j=0;j<s.length;j++){
								if(!reg.test(s[j])){
									i+=2;
								}else{
									i++;	
								}
							}
							reg=new RegExp("[_]");
							if(!reg.test(s)){
								reg=new RegExp("[A-Za-z0-9\u4e00-\u9fa5]{5,30}$");
								if(!reg.test(s)){
									insertErr("不能输入符号",$this);	
									pass[i]=0;
								}else{
									$this.next("span").remove();
									pass[i]=1;
								}
							}else {
								insertErr("不能输入下划线",$this);	
								pass[i]=0;
							}
							break
					}	
			}
		}
		
		$.fn.tgFileValidation=function(p){
			var $this=$(this);
			var i=getIndex(p,$this);
			var infostr=$this.attr("title");
			$this.focusout(function(){
				$this.next("span").remove();
				if($this.val()){
					pass[i]=1;
				}else{
					function insertErr(str,obj){
						if(infostr){
							$err.clone().html(infostr).insertAfter(obj);
						}else{
							$err.clone().html(str).insertAfter(obj);
						}	
					}
					insertErr("请上传图片",$this);
					pass[i]=0;
				}
			});
			
		}
		
		$.fn.tgRandCValidation=function(p){
			var $this=$(this);
			var infostr=$this.attr("title");
			var name=$this.attr("name");
			var $these=$("input[name='"+name+"']");
			var i=getIndex(p,$this);
			var l=$these.length;
			$these.focusout(function(){
				$these.eq(l-1).next("label").next("span").remove();
				if($("input[name='"+name+"']:checked").length==0){
					pass[i]=0;
					if(infostr){
						$err.clone().html(infostr).insertAfter($these.eq(l-1).next("label"));
					}else{
						$err.clone().html("至少选择一项").insertAfter($these.eq(l-1).next("label"));
					}
				}else{
					pass[i]=1;
				}
			});	
		}
		
		$.fn.tgSValidation=function(p){
			var $this=$(this);
			var i=getIndex(p,$this);
			var infostr=$this.attr("title");
			if($this.attr("or")){
				var s=$("#"+$this.attr("or")).val().replace(/(^\s*)|(\s*$)/g,"");
			}else{
				var s="";
			}
			$this.change(function(){
				$this.next("span").remove();
				if(!s){
					if($this.find("option").eq(0).is(":selected")){
						pass[i]=0;
						if(infostr){
		  					$err.clone().html(infostr).insertAfter($this);
		  				}else{
		  					$err.clone().html("必选项").insertAfter($this);
		  				}	
					}else{
						pass[i]=1;
					}
				}else{
					pass[i]=1;
				}
			});
		}
		
		$.fn.tgFormValidation=function(){
			var $this=$(this);
			$this.find("input[type='text'][isvali='true'],input[type='hidden'][isvali='true']").each(function(){
				$(this).tgTextValidation($this);
			});
			$this.find("input[type='file'][isvali='true']").each(function(){
				$(this).tgFileValidation($this);
			});
			$this.find("input[type='radio'][isvali='true']").each(function(){
				$(this).tgRandCValidation($this);
			});
			$this.find("input[type='checkbox'][isvali='true']").each(function(){
				$(this).tgRandCValidation($this);
			});
			$this.find("select[isvali='true']").each(function(){
				$(this).tgSValidation($this);
			});
			$this.submit(function(){
				
				pass=new Array();
				$this.find("input[isvali='true']").each(function(){
					$(this).trigger("focusout");
				});
				
				$this.find("select[isvali='true']").each(function(){
					$(this).trigger("change");
				});
				for(i in pass){
					ispass=ispass*pass[i];	
				}
				if(ispass==0){
					for(i in pass){
						if(pass[i]==0){
							if($this.find("*[isvali='true']").eq(i).attr("type")=="hidden"){
								var forstr=$this.find("*[isvali='true']").eq(i).attr("forinput");
								if(forstr){
									$("#"+forstr)[0].focus();
								}else{
									$this.find("*[isvali='true']")[i+1].focus();
								}
							}else{
								$this.find("*[isvali='true']")[i].focus();
							}
							return false;	
						}	
					}
				}else{
				}
			});
			
		}
		function getIndex(p,c){
			var i=p.find("*[isvali='true']").index(c);
			return i;				
		}
})(jQuery);